package com.unionpay.upyzt.example;

import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CombinedGuaranteeDeposit;
import com.unionpay.upyzt.resp.CombinedGuaranteeDepositResp;
import com.unionpay.upyzt.resp.GuaranteeDepositResp;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CombinedGuaranteeDepositExample {

    public CombinedGuaranteeDepositExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    protected String balanceAcctId;

    /**
     * 运行 Demos
     *
     * @param balanceAcctId 电子账簿ID
     */
    public static String runDemos(String balanceAcctId) throws UpyztException {
        CombinedGuaranteeDepositExample combinedGuaranteeDepositExample = new CombinedGuaranteeDepositExample(balanceAcctId);
        // 合并支付担保下单
        CombinedGuaranteeDepositResp resp = combinedGuaranteeDepositExample.create();
        // 合并支付担保下单订单查询（系统订单号）
        resp = combinedGuaranteeDepositExample.retrieveById(resp.getCombinedGuaranteeDepositId());
        // 合并支付担保下单订单查询（平台订单号）
        resp = combinedGuaranteeDepositExample.retrieveByOutOrderNo(resp.getOutOrderNo());
        // 合并支付担保下单子订单查询（平台订单号）
        GuaranteeDepositResp guaranteeDepositResp = combinedGuaranteeDepositExample.retrieveByGuaranteeDepositId(resp.getGuaranteeDepositResults().get(0).getGuaranteeDepositId());
        return guaranteeDepositResp.getGuaranteeDepositId();
    }

    /**
     * 合并支付担保下单
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeDepositResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("combined_out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("deposit_type", "1");
        params.put("payment_type", "SDK_TEST");
        params.put("payment_trade_no", Main.generateRandomOutNo());
        params.put("payment_succeeded_at", OffsetDateTime.now().toString());
        List<Map<String, Object>> guaranteeDepositParams = new ArrayList<>(1);
        Map<String, Object> guaranteeDepositParam = new HashMap<>();
        guaranteeDepositParam.put("amount", 100);
        guaranteeDepositParam.put("balance_acct_id", this.balanceAcctId);
        guaranteeDepositParam.put("out_order_no", Main.generateRandomOutNo());
        guaranteeDepositParam.put("remark", "用于 SDK 示例测试");
        guaranteeDepositParam.put("metadata", MockData.metadata());
        guaranteeDepositParam.put("extra", MockData.extra());
        guaranteeDepositParams.add(guaranteeDepositParam);
        params.put("guarantee_deposit_params", guaranteeDepositParams);
        System.out.println("------- 合并支付担保下单 -------");
        CombinedGuaranteeDepositResp resp = CombinedGuaranteeDeposit.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并支付担保下单订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeDepositResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 合并支付担保下单订单查询（系统订单号） -------");
        CombinedGuaranteeDepositResp resp = CombinedGuaranteeDeposit.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并支付担保下单订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteeDepositResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 合并支付担保下单订单查询（平台订单号） -------");
        CombinedGuaranteeDepositResp resp = CombinedGuaranteeDeposit.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

    public GuaranteeDepositResp retrieveByGuaranteeDepositId(String id) throws UpyztException {
        System.out.println("------- 合并支付担保下单子订单查询（平台订单号） -------");
        GuaranteeDepositResp resp = CombinedGuaranteeDeposit.retrieveByGuaranteeDepositId(id);
        System.out.println(resp);
        return resp;
    }

}
